//
//  SearchOsnIdVC.swift
//  YueChat
//
//  Created by 1 on 2024/10/31.
//

import UIKit

class SearchOsnIdVC: BaseVC {

    @IBOutlet weak var textField: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.title = "加好友/群"
        self.view.backgroundColor = kGrayBG
    }

    @IBAction func onSearch(_ sender: Any) {
        let text = textField.text ?? ""
        if  text.count == 0 {
            MHAlert.showMessage("输入内容不能为空")
            return
        }
        
        if text.hasPrefix("OSNU") {
            let userInfo = gosnUser.getUserInfoDefault(text)
            if userInfo.userId == gosnUser.myId() {
                self.navigationController?.pushViewController(MySetVC(), animated: true)
            }else if gosnFriend.isFriend(userInfo.userId) {
                let detailViewController = UserDetailVC(userInfo: userInfo)
                navigationController?.pushViewController(detailViewController, animated: true)
            } else {
                let addFriendViewController = AddUserVC(userInfo: userInfo)
                navigationController?.pushViewController(addFriendViewController, animated: true)
            }
        } else if text.hasPrefix("OSNG") {
            let convInfo = gosnConversation.getConversationInfo(text)
            if convInfo != nil {
                let vc = ChatVC(conversation: convInfo!)
                navigationController?.pushViewController(vc, animated: true)
            } else {
                let groupInfo = gosnGroup.getGroupInfoDefault(text) // Assuming this is the correct initializer
                let vc = AddGroupVC(group: groupInfo)
                navigationController?.pushViewController(vc, animated: true)
            }
        }else {
            MHAlert.showMessage("无效的OsnId")
        }
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
